//
//  LGSportsPlanDetailController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/31.
//  运动方案--详情

#import "LGSportsPlanDetailController.h"
//编辑运动方案
#import "LGSportsPlanCreateController.h"
//发放记录
#import "LGSportsSendRecordController.h"
//发放方案
#import "LGSportsSendMemberController.h"
//头
#import "LGRecoveryDetailHeadView.h"
#import "LGRecoverySectionView.h"
#import "LGRecoveryTrainingCell.h"
@interface LGSportsPlanDetailController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)LGRecoveryDetailHeadView *headView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIButton *editBtn;
/**   */
@property (nonatomic , strong)UIButton *sendBtn;
/**   */
@property (nonatomic , strong)NSDictionary *dataDic;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGSportsPlanDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"运动方案";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.editBtn];
    [self.view addSubview:self.sendBtn];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/sport.plan/detail" parameters:@{@"plan_id":self.planid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.dataDic = returnData;
            self.headView.dataDic = returnData;
            self.dataArray = returnData[@"actions"];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//发放记录
-(void)checkSendRecordAction{
    LGSportsSendRecordController *controller = [[LGSportsSendRecordController alloc]init];
    controller.planid = self.planid;
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.sendRecordChanged = ^{
        if(weakSelf.sportsPlanChanged){
            weakSelf.sportsPlanChanged();
        }
    };
}

//编辑
-(void)editSportsPlanAction{
    LGSportsPlanCreateController *controller = [[LGSportsPlanCreateController alloc]init];
    controller.planid = LGNSString(self.dataDic[@"id"]);
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.saveDataSuccess = ^{
        [weakSelf requestData];
        //刷新列表
        if(weakSelf.sportsPlanChanged){
            weakSelf.sportsPlanChanged();
        }
    };
}

//发放
-(void)sendSportsPlanAction{
    LGSportsSendMemberController *controller = [[LGSportsSendMemberController alloc]init];
    controller.planid = self.planid;
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.sendMemberChanged = ^{
        if(weakSelf.sportsPlanChanged){
            weakSelf.sportsPlanChanged();
        }
    };
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGRecoveryTrainingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.showDesc = NO;
    cell.showTip = NO;
    NSArray *tempArray = self.dataArray[indexPath.section][@"items"];
    if(tempArray.count == 1){
        cell.type = @"separate";
        
    }else if(indexPath.row == 0){
        cell.type = @"top";
        
    }else if (indexPath.row == tempArray.count-1){
        cell.type = @"bottom";
        
    }else{
        cell.type = @"middle";
    }
    cell.dataDic = tempArray[indexPath.row];
    
    NSInteger count = 0;
    for (NSInteger i=0; i<self.dataArray.count; i++) {
        NSArray *tempArray = self.dataArray[i][@"items"];
        if(i<indexPath.section){
            count += tempArray.count;
        }else if (i == indexPath.section){
            count+=indexPath.row;
            break;
        }
    }
    cell.index = count;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return (tempArray.count>0)?viewPix(45):0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    NSDictionary *tempDic = self.dataArray[section];
    if(tempArray.count>0){
        LGRecoverySectionView *headView = [[LGRecoverySectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(45))];
        headView.title = LGNSString(tempDic[@"category_name"]);
        return headView;
    }
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    return (tempArray.count>0)?viewPix(5):0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"items"];
    CGFloat height = (tempArray.count>0)?viewPix(8):0.01;
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(84)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGRecoveryTrainingCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

-(LGRecoveryDetailHeadView *)headView{
    if(!_headView){
        _headView = [[LGRecoveryDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(276))];
        _headView.isCommon = YES;
        [_headView.sendRecord addTarget:self action:@selector(checkSendRecordAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, 0.5)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
    }
    return _lineView;
}

-(UIButton *)editBtn{
    if(!_editBtn){
        _editBtn = [UIButton buttonWithTitle:@"编辑" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#429BFF" selectColor:@"#429BFF"];
        _editBtn.frame = CGRectMake(viewPix(47), kMaxY(self.tableView.frame)+viewPix(13), viewPix(128), viewPix(46));
        _editBtn.layer.borderColor = [UIColor colorWithString:@"#429BFF"].CGColor;
        _editBtn.layer.borderWidth = 1;
        _editBtn.cornerRidus = 10;
        [_editBtn addTarget:self action:@selector(editSportsPlanAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}

-(UIButton *)sendBtn{
    if(!_sendBtn){
        _sendBtn = [UIButton buttonWithTitle:@"发放" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#FFFFFF" selectColor:@"#FFFFFF"];
        _sendBtn.frame = CGRectMake(viewPix(24)+kMaxX(self.editBtn.frame), kMinY(self.editBtn.frame), viewPix(128), viewPix(46));
        _sendBtn.backgroundColor = [UIColor colorWithString:@"#429BFF"];
        _sendBtn.cornerRidus = 10;
        [_sendBtn addTarget:self action:@selector(sendSportsPlanAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
